package com.ruoyi.pcwl.controller;

import java.util.List;
import java.util.Map;


import com.ruoyi.pcwl.domain.dto.HotelDto;
import com.ruoyi.pcwl.domain.enums.MediaTypeEnum;
import com.ruoyi.pcwl.domain.query.HotelQuery;
import com.ruoyi.pcwl.domain.Hotel;
import com.ruoyi.pcwl.domain.vo.Result;
import com.ruoyi.pcwl.minio.MinioUtil;
import com.ruoyi.pcwl.service.HotelService;
import com.ruoyi.pcwl.service.IMediaFilesService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * 酒店信息表 Controller
 */

@Api(tags = "酒店信息表控制层")
@RestController("hotelController")
@RequestMapping("/hotel")
public class HotelController extends BaseController{

	@Resource
	private HotelService hotelService;

	@Value("${minio.bucketName}")
	private String bucketName;

	@Resource
	private IMediaFilesService mediaFilesService;

	/**
	 * 根据条件分页查询
	 */
	@ApiOperation("根据条件分页查询")
	@GetMapping("/loadDataList")
	public Result loadDataList(HotelQuery query){
		return Result.ok(hotelService.selectPage(query));
	}

	/**
	 * 新增
	 */
	@ApiOperation("新增")
	@PostMapping("/add")
	public Result add(HotelDto hotelDto) {
		MultipartFile file = hotelDto.getFile();
		//上传文件到minio
		Map<String, Object> map = null;
		try {
			map = MinioUtil.uploadFile(file, MediaTypeEnum.HOTEL);
		} catch (Exception e) {
			throw new RuntimeException("上传文件失败");
		}

		//保存酒店信息
		Hotel hotel = new Hotel();
		BeanUtils.copyProperties(hotelDto,hotel);
		hotel.setPic(map.get("path").toString());
		hotel.setMediaFileId(Long.parseLong(map.get("mediaFileId").toString()));

		hotelService.add(hotel);
		return Result.ok(null);
	}

	/**
	 * 批量新增
	 */
	@ApiOperation("批量新增")
	@PostMapping("/addBatch")
	public Result addBatch(@RequestBody List<Hotel> listBean) {
		hotelService.addBatch(listBean);
		return Result.ok(null);
	}

	/**
	 * 批量新增/修改
	 */
	@ApiOperation("批量新增/修改")
	@PostMapping("/addOrUpdateBatch")
	public Result addOrUpdateBatch(@RequestBody List<Hotel> listBean) {
		hotelService.addBatch(listBean);
		return Result.ok(null);
	}

	/**
	 * 根据Id查询对象
	 */
	@ApiOperation("根据Id查询对象")
	@GetMapping("/getHotelById/{id}")
	public Result getHotelById(@PathVariable Long id) {
		return Result.ok(hotelService.getHotelById(id));
	}

	/**
	 * 根据Id修改对象
	 */
	@ApiOperation("根据Id修改对象")
	@PutMapping("/updateHotelById")
	public Result updateHotelById(HotelDto hotelDto) {
		if(hotelDto.getFile()==null)
		{
			Hotel bean = new Hotel();
			BeanUtils.copyProperties(hotelDto,bean);
			hotelService.updateHotelById(bean);
			return Result.ok(null);
		}
		else {
			MultipartFile file = hotelDto.getFile();
			//上传文件到minio
			Map<String, Object> map = null;
			try {
				map = MinioUtil.uploadFile(file, MediaTypeEnum.HOTEL);
			} catch (Exception e) {
				throw new RuntimeException("上传文件失败");
			}

			//保存酒店信息
			Hotel hotel = new Hotel();
			BeanUtils.copyProperties(hotelDto, hotel);
			hotel.setPic(map.get("path").toString());
			hotel.setMediaFileId(Long.parseLong(map.get("mediaFileId").toString()));

			hotelService.updateHotelById(hotel);

			return Result.ok(null);
		}
	}

	/**
	 * 根据Id删除
	 */

	@ApiOperation("根据Id删除")
	@DeleteMapping("/deleteHotelById/{id}")
	public Result deleteHotelById(@PathVariable Long id) {
		hotelService.deleteHotelById(id);
		return Result.ok(null);
	}
}
